﻿$(document).ready(function () {
    //    page.commonUserControl = new NDUAP.Selector({ divID: "userId", typeID: 1, totalSelectNum: 1, quickBoxPositionX: -285, width: 150, searchData: page.userList });
    $("#ul-nav li:not(.nav-header)").eq(2).addClass("active");
    page.init();
});

var page = {
    pageIndex: 0,
    pageSize: 10,
    url: "/handler/admin/integration.ashx?action=",
    popU: null,

    init: function () {
        page.getUserList();
    },

    getUserList: function (pageIndex) {
        var user = $.trim($("#userName").val());
        var deptName = $.trim($("#deptName").val());
        var jobName = $.trim($("#jobName").val());
        var gender = $("#gender").val();
        var usertype = $("#usertype").val();

        if (pageIndex != undefined)
            page.pageIndex = pageIndex;

        $.get(page.url + "integration", { user: user, gender: gender, deptName: deptName,
            jobName: jobName, usertype: usertype,
            pageIndex: page.pageIndex, pageSize: page.pageSize, r: Math.random()
        }, function (data) {
            $("#result").html(TrimPath.processDOMTemplate("userList", data));
            page.pager(data.count);
            page.list = data.list;
        }, "json");
    },

    pager: function (count) {
        if (count == 0) {
            $("#pagination").empty();
            return;
        }
        $("#pagination").pagination(count, {
            num_edge_entries: 2,
            num_display_entries: 10,
            current_page: page.pageIndex,
            items_per_page: page.pageSize,
            callback: page.getUserList
        });
    },

    title: function (type) {
        switch (type) {
            case 1:
                return '分配可用素币';
            case 2:
                return '扣除可用素币';
            case 3:
                return '分配特殊素币';
            case 4:
                return '扣除特殊素币';
        }
        return '';
    },

    edit: function (type, username, jobnumber, userId) {
        var html = [];
        html.push('<form class="form-horizontal">');
        html.push('<div class="control-group"><label class="control-label">员工</label>');
        html.push('<div class="controls"><label class="control-label" style="width:240px; text-align:left;">' + username + '<small>(' + jobnumber + ')</small></label></div>');
        html.push('</div>');
        html.push('<div class="control-group"><label class="control-label" for="amount">素币数</label>');
        html.push('<div class="controls"><input id="amount" placeholder="素币数" type="text" required/></div>');
        html.push('</div>');
        html.push('<div class="control-group"><label class="control-label" for="description">备注</label>');
        html.push('<div class="controls"><textarea id="description" placeholder="备注" rows="3"></textarea></div>');
        html.push('</div>');
        html.push('<div class="control-group">');
        html.push('<div class="controls"><button class="btn" type="button" onclick="page.reset(' + type + ',' + userId + ')">确认</button></div>');
        html.push('</div>');
        html.push('</form>');


        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: page.title(type),
            contents: html.join(""),
            w: 400,
            drag: true
        });
        page.popU.showPop();

        $("#amount").keyup(function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).bind("paste", function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).css("ime-mode", "disabled");
    },

    reset: function (type, userId) {
        var amount = $.trim($("#amount").val());

        if (amount == "") {
            alert("素币数不能为空");
            $("#amount").focus();
            return;
        }

        $.post(page.url + "grantIntegration", { userId: userId, type: type, amount: amount, description: $("#description").val()}, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.getUserList();
            page.popU.close();
        }, "json");
    }
};

